package org.kms.Controllers;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.kms.dao.kmsPermissionDao;
import org.kms.pojo.KmsPermission;
import org.kms.pojo.KmsUser;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Response;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Permission")
@CrossOrigin("*")

public class Permission {
    @Autowired
    private kmsPermissionDao kmsPermissionDao;


    @RequestMapping("/TreeSimple")
    @ResponseBody
    public List<KmsPermission> getTreeSimple() throws Exception{
        List<KmsPermission> PerResultRows = kmsPermissionDao.getPermissionSimpleTree();
        return PerResultRows;
    }

    @RequestMapping("/all")
    @ResponseBody
    public List<KmsPermission> getperall() throws Exception{
        return kmsPermissionDao.getAll();
    }

    @RequestMapping("/Trees")
    @ResponseBody
    public List<KmsPermission> getTrees() throws Exception{
        return kmsPermissionDao.getfirstmenu();
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseResult add(Integer roleId, String perId)throws Exception{
        List<Integer> perid = new ArrayList<Integer>();
        String [] p = perId.split(",");
        for (int i = 0; i < p.length; i++) {
            perid.add(Integer.parseInt(p[i]));
        }
        kmsPermissionDao.addPermission(roleId,perid);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/rolePerid/{roleId}")
    @ResponseBody
    public List<Integer> getPermissionid(@PathVariable Integer roleId)throws Exception{
        return kmsPermissionDao.getPermissionsbypid(roleId);
    }

    @RequestMapping("/Permissiondel")
    @ResponseBody
    public ResponseResult delper(Integer roleId)throws Exception{
        kmsPermissionDao.delPer(roleId);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/Peryiji")
    @ResponseBody
    public ResponseResult getperyi()throws Exception{
        List<KmsPermission> firstPerList =kmsPermissionDao.getfirstmenu();
        return new ResponseResult(firstPerList);
    }
    @RequestMapping("/Pererji")
    @ResponseBody
    public ResponseResult getperer(Integer perId)throws Exception{
        List<KmsPermission> secondPerList =kmsPermissionDao.getbyerji(perId);
        return new ResponseResult(secondPerList);
    }

    @PostMapping("/PerImg")
    @ResponseBody
    public ResponseResult getperera(MultipartFile file,HttpServletRequest request)throws Exception{

        String path = "F:\\Users\\Administrator\\IdeaProjects\\Kms-Project\\Project\\KindergartenMS\\KindergartenMS-Ui\\src\\main\\webapp\\images\\permission\\";

      /*  String path =request.getSession().getServletContext().getRealPath("")+File.separator;
        System.out.println(path);*/
        String perimg = file.getOriginalFilename();
        File file1 = new File(path,perimg);
        file.transferTo(file1);
        return ResponseResult.createSuccessResult();
    }
    @PostMapping("/Peradd")
    @ResponseBody
    public ResponseResult getperer(KmsPermission kmsPermission)throws Exception{
        kmsPermissionDao.add(kmsPermission);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/Perpdjb")
    @ResponseBody
    public ResponseResult pdkmsper(Integer perId)throws Exception{
        KmsPermission kmsPermission = kmsPermissionDao.getbykmspd(perId);
        return new ResponseResult(kmsPermission);
    }
    @RequestMapping("/Peroneji")
    @ResponseBody
    public ResponseResult Peroneji(Integer perId)throws Exception{

        KmsPermission Peroneji = kmsPermissionDao.getbyoneji(perId);
        return new ResponseResult(Peroneji);
    }
    @RequestMapping("/Pertwoji")
    @ResponseBody
    public ResponseResult Pertwoji(Integer perId)throws Exception{
        KmsPermission Pertwoji = kmsPermissionDao.getbytwoji(perId);
        return new ResponseResult(Pertwoji);
    }
    @RequestMapping("/Perthreeji")
    @ResponseBody
    public ResponseResult Perthreeji(Integer perId)throws Exception{
        KmsPermission Perthreeji = kmsPermissionDao.getbythreeji(perId);
        return new ResponseResult(Perthreeji);
    }
    @RequestMapping("/PergetbyoneId")
    @ResponseBody
    public ResponseResult PergetbyoneId(Integer perId)throws Exception{

        Integer PergetbyoneId =kmsPermissionDao.getbyoneperId(perId);
        return new ResponseResult(PergetbyoneId);
    }
    @RequestMapping("/PergetbytwoId")
    @ResponseBody
    public ResponseResult PergetbytwoId(Integer perId)throws Exception{
        Integer PergetbytwoId =kmsPermissionDao.getbytwoperId(perId);
        return new ResponseResult(PergetbytwoId);
    }

    @RequestMapping("/Perupdate")
    @ResponseBody
    public ResponseResult updpermission(KmsPermission kmsPermission)throws Exception{
        kmsPermissionDao.updpermission(kmsPermission);
        return ResponseResult.createSuccessResult();
    }
   @RequestMapping("/Perdelyiji")
    @ResponseBody
    public ResponseResult delpermissionyiji(Integer perId){

        List<Integer> p3 =kmsPermissionDao.getbythreeperida(perId);
     if (p3.size()>0){
         for (Integer ps:p3
         ) {
             kmsPermissionDao.delpermission2(ps);
         }
     }
        List<Integer> p2 =kmsPermissionDao.getbytwoperida(perId);
        if (p2.size()>0){
       for (Integer p:p2
            ) {
           kmsPermissionDao.delpermission1(p);
       }
        }
      kmsPermissionDao.delpermission(perId);

        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/Perdelerji")
    @ResponseBody
    public ResponseResult delpermissionerji(Integer perId){

        List<Integer> p3 =kmsPermissionDao.getbythreeperida(perId);
        if (p3.size() >0){
        for (Integer ps:p3
        ) {
            kmsPermissionDao.delpermission2(ps);
        }
        }
        kmsPermissionDao.delpermission1(perId);

        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/Perdelsanji")
    @ResponseBody
    public ResponseResult delpermissionsanji(Integer perId){

        kmsPermissionDao.delpermission2(perId);
        return ResponseResult.createSuccessResult();
    }

}
